<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class Order
 * Desc  订单控制器
 * Create on 2025/5/20 14:00
 * Create by wangyafang
 */

namespace app\api\controller\wddj;


use app\api\model\wddj\user\Payments;
use app\common\controller\Api;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use Exception;

class Order extends Api
{
    protected $noNeedLogin = ['awaiting_payment_scan_notify'];
    protected $noNeedRight = ['*'];

    protected $model = null;
    protected $driverObj = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\user\Order();
    }

    /**
     * Desc 补差二维码回调
     * Create on 2025/5/20 14:01
     * Create by wangyafang
     */
    public function awaiting_payment_scan_notify()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $pay = \addons\wddj\library\Wxapp::getPay();
        $response = $pay->handlePaidNotify(function($message, $fail){
            $paymentsModel = new Payments();
            $paymentsObj = $paymentsModel
                ->where('out_trade_no',$message['out_trade_no'])
                ->where('payment_type','awaiting_payment')
                ->find();
            if (!$paymentsObj || $paymentsObj['payment_status'] == 'completed') {
                return true;
            }
            if ($message['return_code'] === 'SUCCESS') {
                if ($message['result_code'] === 'SUCCESS') {
                    $this->notify_inner($paymentsObj);
                }
            } else {
                return $fail('FAIL');
            }

            return true;
        });

        $response->send();
    }

    protected function notify_inner($paymentsObj)
    {
        $orderObj = $this->model->get($paymentsObj['order_id']);
        $order_data_update = array(
            'state'=>'completed',
            'pay_amount'=>bcadd($orderObj['pay_amount'],$paymentsObj['pay_amount'],2),
            'completion_time'=>date('Y-m-d H:i:s',time()),
        );
        Db::startTrans();
        try {
            $orderObj->save($order_data_update);
            $paymentsObj['pay_time'] = time();
            $paymentsObj['payment_status'] = 'completed';
            $paymentsObj->save();
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }
}



 